package requests

import (
	"WeChatMGRA/modul"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

var urlhead = "http://134.175.73.113:8080/"
var authorization = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlRWJLODcxRFZDTGhJbCh6TyIsImlzcyI6InhpbmdzaGVuZyIsImFjY291bnQiOiIxODcyMjYyNjExMCJ9._GDQ5kANgtC8Zon0_3BAp-Jz0k21H58Jq8e_CU4fjjc"

//MakeARequest 请求一个url,并将内容返回
func MakeARequest(urltail string,body []byte) ([]byte, error){
	req := fasthttp.AcquireRequest()
	defer fasthttp.ReleaseRequest(req) // 用完需要释放资源
	req.Header.SetContentType("application/json")
	req.Header.SetMethod("POST")
	req.Header.Add("Authorization",authorization)
	req.SetBody(body)
	req.SetRequestURI(urlhead+urltail)
	resp := fasthttp.AcquireResponse()
	defer fasthttp.ReleaseResponse(resp)
	if err := fasthttp.Do(req, resp); err != nil {
		return nil,err
	}
	resp_data := resp.Body()
	return  resp_data,nil
}
//VerifyWKResp 验证一个返回值，并解析code  和 解析到某个具体的结构体
func VerifyWKResp(data []byte,obj interface{})(bmsg * modul.BaseMsg,err error){
	bmsg = new(modul.BaseMsg)
	if err = json.Unmarshal(data,bmsg);err!=nil {
		return
	}
	obj_data,err:= json.Marshal(bmsg.Data)
	if err!=nil{
		return bmsg,err
	}
	if obj==nil{
		return
	}
	err = json.Unmarshal(obj_data,obj)
	return
}

